package com.ffcs.cn.action.scene;

import com.supermap.data.*;
import com.supermap.desktop.core.Application;
import com.supermap.desktop.core.Interface.IBaseItem;
import com.supermap.desktop.core.Interface.IFormScene;
import com.supermap.desktop.core.geometryDrawing.common.DrawingCursorFactory;
import com.supermap.desktop.core.implement.CtrlAction;
import com.supermap.desktop.core.utilties.CursorUtilities;
import com.supermap.desktop.core.utilties.FontUtilities;
import com.ffcs.cn.DevelopProperties;
import com.supermap.realspace.PixelToGlobeMode;
import com.supermap.realspace.SceneType;
import com.supermap.ui.Action3D;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.MessageFormat;

/**
 * 查询场景鼠标位置的坐标值
 *
  */
public class CtrlActionPositionQuery extends CtrlAction {
	private static final String CURRENT_POSITION_INFO = "CURRENT_POSITION_INFO";
	private static final String CURRENT_OBJECT = "CURRENT_OBJECT";
	private static final String CURRENT_OBJECT_TEXT = "CURRENT_OBJECT_TEXT";
	/**
	 * 监听鼠标移动，查询当前坐标值
	 */
	MouseAdapter motionAdapter = new MouseAdapter() {
		@Override
		public void mouseMoved(MouseEvent e) {
			IFormScene formScene = (IFormScene) Application.getActiveApplication().getMainFrame().getFormManager().getActiveForm();
			int index = formScene.getSceneControl().getScene().getTrackingLayer().indexOf(CURRENT_POSITION_INFO);
			GeoText3D currentInfoPosition;
			if (index == -1) {
				TextStyle textStyle = new TextStyle();
				textStyle.setFontHeight(4);
				textStyle.setAlignment(TextAlignment.BOTTOMLEFT);
				textStyle.setForeColor(new Color(255, 48, 48));
				textStyle.setOutline(true);
				textStyle.setFontName(FontUtilities.getDefaultFont().getFontName());

				TextPart3D textPart3D = new TextPart3D();
				textPart3D.setAnchorPoint(new Point3D(0, 0, 0));

				GeoStyle3D style3D = new GeoStyle3D();
				style3D.setAltitudeMode(AltitudeMode.ABSOLUTE);

				currentInfoPosition = new GeoText3D(textPart3D, textStyle);
				currentInfoPosition.setStyle3D(style3D);
				index = formScene.getSceneControl().getScene().getTrackingLayer().add(currentInfoPosition, CURRENT_POSITION_INFO);
			} else {
				currentInfoPosition = ((GeoText3D) formScene.getSceneControl().getScene().getTrackingLayer().get(index));
			}
			Point3D point3D = new Point3D(formScene.getSceneControl().getScene().pixelToGlobe(e.getPoint(), PixelToGlobeMode.TERRAINANDMODEL));
			// 当前坐标信息文本
			String position = getPosition(point3D);
			currentInfoPosition.getPart(0).setText(MessageFormat.format(DevelopProperties.getString("String_CurrentPosition"), position));
			currentInfoPosition.getPart(0).setX(point3D.getX());
			currentInfoPosition.getPart(0).setY(point3D.getY());
			currentInfoPosition.getPart(0).setZ(point3D.getZ());

			formScene.getSceneControl().getScene().getTrackingLayer().set(index, currentInfoPosition);
		}
	};

	/**
	 * 鼠标点击监听事件
	 */
	private final MouseAdapter clickAdapter = new MouseAdapter() {
		@Override
		public void mousePressed(MouseEvent e) {
			IFormScene formScene = (IFormScene) Application.getActiveApplication().getMainFrame().getFormManager().getActiveForm();
			if (e.getButton() == MouseEvent.BUTTON3) {
				formScene.getSceneControl().removeMouseMotionListener(motionAdapter);
				formScene.getSceneControl().removeMouseListener(clickAdapter);
				formScene.getSceneControl().setCursorCustomized(false);
				formScene.showPopupMenu();
				formScene.getSceneControl().getScene().getTrackingLayer().clear();
			} else if (e.getButton() == MouseEvent.BUTTON1) {
				Point3D point3D = new Point3D(formScene.getSceneControl().getScene().pixelToGlobe(e.getPoint(), PixelToGlobeMode.TERRAINANDMODEL));
				String currentPosition = getPosition(point3D);
				Application.getActiveApplication().getOutput().output(currentPosition);
				GeoPoint3D geoPoint3D = new GeoPoint3D();
				GeoStyle3D geoStyle3D = new GeoStyle3D();

				geoPoint3D.setStyle3D(geoStyle3D);
				geoPoint3D.setX(point3D.getX());
				geoPoint3D.setY(point3D.getY());
				geoPoint3D.setZ(point3D.getZ());

				TextStyle textStyle = new TextStyle();
				textStyle.setForeColor(Color.green);
				textStyle.setFontName(FontUtilities.getDefaultFont().getFontName());
				textStyle.setFontHeight(4);


				TextPart3D textPart3D = new TextPart3D();
				textPart3D.setAnchorPoint(new Point3D(0, 0, 0));

				GeoText3D geoText3D = new GeoText3D(textPart3D, textStyle);
				geoText3D.getPart(0).setText(currentPosition);
				geoText3D.getPart(0).setX(point3D.getX());
				geoText3D.getPart(0).setY(point3D.getY());
				geoText3D.getPart(0).setZ(point3D.getZ());

				formScene.getSceneControl().getScene().getTrackingLayer().add(geoPoint3D, CURRENT_OBJECT);
				formScene.getSceneControl().getScene().getTrackingLayer().add(geoText3D, CURRENT_OBJECT_TEXT);
			}
		}
	};

	@Override
	protected void run() {
		if (!(Application.getActiveApplication().getMainFrame().getFormManager().getActiveForm() instanceof IFormScene)) {
			return;
		}
		IFormScene formScene = (IFormScene) Application.getActiveApplication().getMainFrame().getFormManager().getActiveForm();
		formScene.getSceneControl().removeMouseMotionListener(motionAdapter);
		formScene.getSceneControl().addMouseMotionListener(motionAdapter);
		formScene.getSceneControl().removeMouseListener(clickAdapter);
		formScene.getSceneControl().addMouseListener(clickAdapter);
		formScene.getSceneControl().setCursorCustomized(true);
		formScene.getSceneControl().setAction(Action3D.PAN2);
		formScene.getSceneControl().setCursor(CursorUtilities.createCustomCursor(DrawingCursorFactory.DRAWING_DEFAULT, new Point(16, 16), "Drawing_Default"));
		formScene.dontShowPopupMenu();
	}

	public CtrlActionPositionQuery(IBaseItem caller) {
		super(caller);
	}

	/**
	 * 获取坐标文本
	 *
	 * @param point3D 坐标点
	 * @return 场景上的坐标文本
	 */
	private String getPosition(Point3D point3D) {
		IFormScene formScene = ((IFormScene) Application.getActiveApplication().getActiveForm());
		String X = "";
		String Y = "";
		String Z = "";
		// 三维球体场景
		if (formScene.getSceneControl().getScene().getSceneType() == SceneType.GLOBE) {
			X = "X: " + point3D.getX() + "; ";
			Y = "Y: " + point3D.getY() + "; ";
			Z = "Z: " + point3D.getZ();
		} else {
			// 局部平面场景
			String StrX = String.valueOf(point3D.getX());
			String StrY = String.valueOf(point3D.getY());
			String StrZ = String.valueOf(point3D.getZ());
			if (!StrX.isEmpty()) {
				X = "X: " + point3D.getX() + ", ";
			}
			if (!StrY.isEmpty()) {
				Y = "Y: " + point3D.getY() + ", ";
			}
			if (!StrZ.isEmpty()) {
				Z = "Z: " + point3D.getZ();
			}
		}
		return X + Y + Z;
	}

	@Override
	public boolean enable() {
		return Application.getActiveApplication().getActiveForm() instanceof IFormScene;
	}
}
